﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using _PLATFORM.DATA;

namespace _PLATFORM.WEB.Controllers
{
    [Authorize(Roles = "Administrator, Reseller")]
    public class FittingTypeAssetController : Controller
    {
        private eCommPlatformRepoEntities db = new eCommPlatformRepoEntities();

        private static decimal FittingTypeID { get; set; }
        private static List<FITTING_TYPE_ASSET> FittingTypeAssets { get; set; }

        Guid userID = DATA.Custom_Classes.User.UserID;

        

        //
        // GET: /FittingTypeAsset/

        public ViewResult Index(string FITTING_TYPE_ASSET_ID)
        {
            // Get All My Fitting Types
            var fittingTypes = db.FITTING_TYPE.Where(f => f.USER_ID == userID).ToList();

            // Does User Use Parent Inventory
            if (DATA.Custom_Classes.User.UseParentInventory)
                // Get My And Parent Fitting Types
                fittingTypes.AddRange(
                    db.FITTING_TYPE.Where(f => f.USER_ID == DATA.Custom_Classes.User.ParentID).ToList());
            
            var filterFittingType = fittingTypes.ToList()[0].FITTING_TYPE_ID;

            

            var fittingTypeAssets = db.FITTING_TYPE_ASSET.ToList();

            if (FITTING_TYPE_ASSET_ID != null)
                filterFittingType = decimal.Parse(FITTING_TYPE_ASSET_ID);

            FittingTypeAssets = fittingTypeAssets.Where(f => f.FITTING_TYPE_ID == filterFittingType).ToList();
            ViewBag.FITTING_TYPE_ASSET_ID = new SelectList(fittingTypes, "FITTING_TYPE_ID", "NAME", filterFittingType);

            return View(FittingTypeAssets);
        }

        //
        // GET: /FittingTypeAsset/Details/5

        public ViewResult Details(decimal id)
        {
            FITTING_TYPE_ASSET fitting_type_asset = db.FITTING_TYPE_ASSET.Find(id);
            return View(fitting_type_asset);
        }

        //
        // GET: /FittingTypeAsset/Create

        public ActionResult Create()
        {
            // My Fitting Types
            var fittingTypes = db.FITTING_TYPE.Where(f => f.USER_ID == userID).ToList();

            // Does User Use Parent Inventory
            if (DATA.Custom_Classes.User.UseParentInventory)
                // Get My And Parent Fitting Types
                fittingTypes.AddRange(
                    db.FITTING_TYPE.Where(f => f.USER_ID == DATA.Custom_Classes.User.ParentID).ToList());

            ViewBag.FITTING_TYPE_ASSET_ID = new SelectList(fittingTypes, "FITTING_TYPE_ID", "NAME");
            ViewBag.USER_ID = new SelectList(db.FRAMEWORK_USER, "USER_ID", "FIRST_NAME");
            return View();
        } 

        //
        // POST: /FittingTypeAsset/Create
        
        [HttpPost]
        public ActionResult Create(FITTING_TYPE_ASSET fitting_type_asset, HttpPostedFileBase fileBase, FormCollection collection)
        {
            if (ModelState.IsValid)
            {
                
                // Get The Image
                HttpPostedFileBase mFile = Request.Files[0];
                fitting_type_asset.IMAGE = userID + "_" + mFile.FileName;
                
                // Fitting Type
                var fittingType = collection["FITTING_TYPE_ASSET_ID"];
                fitting_type_asset.FITTING_TYPE_ID = decimal.Parse(fittingType);

                fitting_type_asset.USER_ID = userID;
                db.FITTING_TYPE_ASSET.Add(fitting_type_asset);

                db.SaveChanges();

                // Only Write Image If DB Insert Is OK
                var imagePath = Server.MapPath("~\\Assets\\CategoryImages\\" + fitting_type_asset.IMAGE);
                mFile.SaveAs(imagePath);

                return RedirectToAction("Index");  
            }

            ViewBag.FITTING_TYPE_ASSET_ID = new SelectList(db.FITTING_TYPE, "FITTING_TYPE_ID", "NAME", fitting_type_asset.FITTING_TYPE_ASSET_ID);
            ViewBag.USER_ID = new SelectList(db.FRAMEWORK_USER, "USER_ID", "FIRST_NAME", fitting_type_asset.USER_ID);
            return View(fitting_type_asset);
        }
        
        //
        // GET: /FittingTypeAsset/Edit/5
 
        public ActionResult Edit(decimal id)
        {
            FITTING_TYPE_ASSET fitting_type_asset = db.FITTING_TYPE_ASSET.Find(id);
            ViewBag.FITTING_TYPE_ASSET_ID = new SelectList(db.FITTING_TYPE, "FITTING_TYPE_ID", "NAME", fitting_type_asset.FITTING_TYPE_ASSET_ID);
            ViewBag.USER_ID = new SelectList(db.FRAMEWORK_USER, "USER_ID", "FIRST_NAME", fitting_type_asset.USER_ID);
            return View(fitting_type_asset);
        }

        //
        // POST: /FittingTypeAsset/Edit/5

        [HttpPost]
        public ActionResult Edit(FITTING_TYPE_ASSET fitting_type_asset, FormCollection collection, IEnumerable<HttpPostedFileBase> fileBase, string fittingTypeID)
        {
            var ftaID = collection["id"].Substring(10).Trim('}');
            var val = collection["value"];
            fitting_type_asset.FITTING_TYPE_ASSET_ID = decimal.Parse(ftaID);
            fitting_type_asset.NAME = val;
            fitting_type_asset.USER_ID = _PLATFORM.DATA.Custom_Classes.User.UserID;
            fitting_type_asset.FITTING_TYPE_ID = FittingTypeID;

            var ctr = 0;
            var imgChanged = false;

            foreach (var files in Request.Files)
            {
                var ticks = DateTime.Now.Ticks.ToString();

                HttpPostedFileBase file = Request.Files[ctr];
                if (file.ContentLength > 0)
                {
                    var pathString = "~/Content/User/";

                    if (file.ContentType.Contains("jp") | file.ContentType.Contains("png") | file.ContentType.Contains("gif"))
                    {
                        fitting_type_asset.IMAGE = ticks + file.FileName;
                        pathString += "Images";
                        imgChanged = true;
                    }


                    var savepath = Server.MapPath(Url.Content(pathString));
                    if (!Directory.Exists(savepath))
                        Directory.CreateDirectory(savepath);
                    var filePath = Path.Combine(@savepath, Path.GetFileName(ticks + file.FileName));
                    file.SaveAs(filePath);
                }
                ctr++;
            }

            if (!imgChanged)
            {
                var id = decimal.Parse(ftaID);
                fitting_type_asset.IMAGE = FittingTypeAssets.Where(f => f.FITTING_TYPE_ASSET_ID == id).ToList()[0].IMAGE;
            }

                

            if (ModelState.IsValid)
            {
                db.Entry(fitting_type_asset).State = EntityState.Modified;
                db.SaveChanges();
                return Content(val);
                
            }
            ViewBag.FITTING_TYPE_ASSET_ID = new SelectList(db.FITTING_TYPE, "FITTING_TYPE_ID", "NAME", fitting_type_asset.FITTING_TYPE_ASSET_ID);
            ViewBag.USER_ID = new SelectList(db.FRAMEWORK_USER, "USER_ID", "FIRST_NAME", fitting_type_asset.USER_ID);
            return View(fitting_type_asset);
        }

        //
        // GET: /FittingTypeAsset/Delete/5
 
        public ActionResult Delete(decimal id)
        {
            FITTING_TYPE_ASSET fitting_type_asset = db.FITTING_TYPE_ASSET.Find(id);
            return View(fitting_type_asset);
        }

        //
        // POST: /FittingTypeAsset/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(decimal id)
        {            
            FITTING_TYPE_ASSET fitting_type_asset = db.FITTING_TYPE_ASSET.Find(id);
            db.FITTING_TYPE_ASSET.Remove(fitting_type_asset);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}